#Licensed under the Apache License, Version 2.0 (the "License"); you 
#may not use this file except in compliance with the License. You may obtain 
#a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless 
#required by applicable law or agreed to in writing, software distributed 
#under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 
#OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
#the specific language governing permissions and limitations under the License. 

#Example bash script to run Dragster loadgen in a Load and Performance Environment

#!/bin/bash

HOST_NAME=http://www.

TOMCAT_STARTUP_WAIT_TIME=120
TOMCAT_SHUTDOWN_WAIT_TIME=60
LOG_OUTPUT_DIR=/var/log/performance_logs
TOMCAT_LOG_FILE=/apache-tomcat/logs/catalina.out
DRAGSTER_LOG_FILE=/var/log/performance_logs/dragster.out
DRAGSTER_COMMAND="java -Dhost=$HOST_NAME -cp dragster.jar eu.fastl.dragster.loadgen.HttpLoadGeneratorExec "
DOS_TIME=600
DOS_URL=http://www.


TOMCAT_START_CMD="/apache-tomcat/bin/startup.sh"
TOMCAT_STOP_CMD="/apache-tomcat/bin/shutdown.sh"

echo "Running performance tests with the following configuration:"
echo
echo TOMCAT_START_CMD=$TOMCAT_START_CMD
echo TOMCAT_STOP_CMD=$TOMCAT_STOP_CMD
echo TOMCAT_STARTUP_WAIT_TIME=$TOMCAT_STARTUP_WAIT_TIME
echo TOMCAT_SHUTDOWN_WAIT_TIME=$TOMCAT_SHUTDOWN_WAIT_TIME
echo LOG_OUTPUT_DIR=$LOG_OUTPUT_DIR
echo TOMCAT_LOG_FILE=$TOMCAT_LOG_FILE
echo DRAGSTER_LOG_FILE=$DRAGSTER_LOG_FILE
echo DRAGSTER_COMMAND=$DRAGSTER_COMMAND
echo HOST_NAME=$HOST_NAME

function startTomcats {
	echo "Starting Tomcat..."
	$TOMCAT_START_CMD &
	echo Waiting $TOMCAT_STARTUP_WAIT_TIME seconds for tomcat to startup
	sleep $TOMCAT_STARTUP_WAIT_TIME
}

function stopTomcats {
	echo "Stopping Tomcat..."
	$TOMCAT_STOP_CMD &
	echo Waiting $TOMCAT_SHUTDOWN_WAIT_TIME seconds for tomcat to shutdown
	sleep $TOMCAT_SHUTDOWN_WAIT_TIME
}


#Parameters:
#$1 Scenario name
#$2 Dragster loadgen command
function runScenario {
	
	local TARGET_CATALINA_OUT=$LOG_OUTPUT_DIR/$1.$(date +%Y%m%d).catalina.out
	local TARGET_DRAGSTER_OUT=$LOG_OUTPUT_DIR/$1.$(date +%Y%m%d).dragster.out
	
	echo
	echo
	echo ------------------------------------------------------
	
	echo Starting $1
	
	echo TARGET_CATALINA_OUT=$TARGET_CATALINA_OUT
	echo TARGET_DRAGSTER_OUT=$TARGET_DRAGSTER_OUT
	
	echo Deleting $TOMCAT_LOG_FILE
	rm $TOMCAT_LOG_FILE
	echo Deleting $DRAGSTER_LOG_FILE
	rm $DRAGSTER_LOG_FILE
	
	
	startTomcats
	
	echo running dragster command: $2
	
	$2 &> $DRAGSTER_LOG_FILE 
	
	
	stopTomcats
	
	echo copying $TOMCAT_LOG_FILE to $TARGET_CATALINA_OUT
	scp -B -q $TOMCAT_LOG_FILE $TARGET_CATALINA_OUT
	echo copying $DRAGSTER_LOG_FILE to $TARGET_DRAGSTER_OUT
	scp -B -q $DRAGSTER_LOG_FILE $TARGET_DRAGSTER_OUT
	
	echo $1 finished
	echo ------------------------------------------------------
}

runScenario SCENARIO_1 "$DRAGSTER_COMMAND -t 25 -f scenario1.urls"
runScenario SCENARIO_2_Denial_of_Service_Attack "$DRAGSTER_COMMAND -t 100 -s $DOS_TIME -n 0 -u $DOS_URL"